import { getAjax, deleteAjax } from '@/utils/request'
import { Mutation, Action } from 'vuex'
import { DictData } from './dictData'

export interface OperLog {
  operId: number;
  title: string;
  businessType: string;
  businessTypes: Array<number>;
  method: string;
  requestMethod: string;
  operatorType: string;
  operName: string;
  deptName: string;
  operUrl: string;
  operIp: string;
  operLocation: string;
  operParam: string;
  status: number;
  errorMsg: string;
  operTime: Date;
  beginTime: Date;
  endTime: Date;
}

export interface StateType {
  data: OperLog;
  typeOptions: Array<DictData>;
}

export interface ModuleType {
  namespaced: boolean;
  state: StateType;
  mutations: {
    SET_TYPE_OPTIONS: Mutation<StateType>;
    SET_DATA: Mutation<StateType>;
  };
  actions: {
    getList: Action<StateType, StateType>;
    deleteOperLog: Action<StateType, StateType>;
    cleanOperLog: Action<StateType, StateType>;
    getTypeOptions: Action<StateType, StateType>;
  };
}

const storeModel: ModuleType = {
  namespaced: true,
  state: {
    data: {
      operId: -1,
      title: '',
      businessType: '',
      businessTypes: [],
      method: '',
      requestMethod: '',
      operatorType: '',
      operName: '',
      deptName: '',
      operUrl: '',
      operIp: '',
      operLocation: '',
      operParam: '',
      status: 0,
      errorMsg: '',
      operTime: new Date(),
      beginTime: new Date(),
      endTime: new Date()
    },
    typeOptions: [{
      dictLabel: '',
      dictValue: ''
    }]
  },
  mutations: {
    SET_TYPE_OPTIONS: (state, data) => {
      state.typeOptions = data
    },
    SET_DATA: (state, data) => {
      state.data = data
    }
  },
  actions: {
    getList(_, data) {
      return getAjax('/monitor/operlog/list', data)
    },
    deleteOperLog(_, ids) {
      return deleteAjax(`/monitor/operlog/${ids}`, null)
    },
    cleanOperLog(_) {
      return deleteAjax('/monitor/operlog/clean', null)
    },
    getTypeOptions({ commit, dispatch }) {
      dispatch('dictData/getDicts', 'sys_oper_type', { root: true }).then(res => {
        commit('SET_TYPE_OPTIONS', res.data)
      })
    }
  }
}

export default storeModel
